package com.neuedu.dao;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import com.neuedu.po.User;
import com.neuedu.utils.DataSourceUtils;
import com.neuedu.utils.UUIDUtils;


public class UserDao {

	public User getUserByUsername(String username) throws SQLException {
		
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "select * from user where username = ? ";
		User user = qr.query(sql, new BeanHandler<>(User.class),username);
		System.out.println(sql);
		
		return user;
		
	}

	public void save(User user) throws SQLException {
		
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "insert into user values(?,?,?,?,?,?)";
		qr.update(sql,UUIDUtils.getId(),user.getUsername(),user.getPassword(),user.getName(),user.getPhone(),user.getEmail());
		
	}

	public User getUserByUnAndPwd(String username, String password) throws SQLException {
		
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "select * from user where username = ? and password = ?";
		User user = qr.query(sql, new BeanHandler<>(User.class),username,password);
		return user;
		
	}

}
